﻿var oldExtendedSetupGrid = extendedSetupGrid;
extendedSetupGrid = function(grid) {
	oldExtendedSetupGrid(grid);
	var bizProc = eval(eval(grid.ID + "ParentBizProc"));
	if (bizProc != null) {
		for (var pos = 0; pos < bizProc.ClientControls.length; pos++)
			if (bizProc.ClientControls[pos].id == grid.ID)
			return;

		grid.ClientControl = new ClientControl(grid, setGridState, getGridState);
		grid.ClientControl.id = grid.ID;
		bizProc.addClientControl(grid.ClientControl);
		grid.BizProc = bizProc;
	}
}

function setGridState(stateEl)
{
    var g = this.dhtmlObj;
    if (stateEl.getAttribute('refresh') != null) {
		//this.dhtmlObj.refresh();
		var scrollPos = $ntb(g.ID + "_ScrollInfo").value;
		var rc = stateEl.getAttribute('rowCount');
		rc = new Number(rc);

		g.clearData();
		g.clearSurfaces();
		if (scrollPos != "NaN,NaN")
			g.subscribeOnce('DataReady', function() {
				try {
					g.rowCount = rc;
					g.setupGridScrollPosition(scrollPos);
					window.setTimeout(function() { g.setRowCount(rc); g.setupGridScrollPosition(scrollPos); }, 50);
				} catch (e) { }
			}, null, new Array());
		g.dataBind();
       }
	if(stateEl.getAttribute('rowCount') != null)
	{
		var rc = stateEl.getAttribute('rowCount');
		g.setRowCount(rc);
	}
}

function getGridState(stateEl)
{
	var hiddenField = document.getElementById(this.id + "_SelRowInfo");
	stateEl.setAttribute('selectedRows', hiddenField.value);

//	debugger;
	if(this.dhtmlObj.datatable.log.selectNodes("//"+nitobi.xml.nsPrefix+"data/*").length !=0)
	{
		stateEl.setAttribute('updates',this.dhtmlObj.datatable.log.xml);
		this.dhtmlObj.datatable.flushLog();
	}
}

function gridCellFocus(eventArgs) {
	var grid = eventArgs.getSource();
    var cell = eventArgs.getCell();
	var row = cell.getRow();	
	if(grid.syrinxLastRow != row)
	{
	    grid.syrinxLastRow = row;
	    if(grid.datatable.log.selectNodes("//"+nitobi.xml.nsPrefix+"data/*").length !=0)
	    {
		    grid.BizProc.triggerEvent(grid.ClientControl.id, 'bizObjUpdated', this.dhtmlObj.datatable.log.xml);
			this.dhtmlObj.datatable.flushLog();
		}
    }
}

function gridCellEditComplete(eventArgs)
{
	try
	{
		if(eventArgs.getEvent().type == "blur" || eventArgs.getEvent().type == "keydown")
		{										   
			var cell = eventArgs.getCell();
			var col = cell.getColumn();
			var row = cell.getRow();
			var grid = eventArgs.getSource();
			var backward = eventArgs.getEvent().shiftKey;
			var keyCode = eventArgs.getEvent().keyCode;
			//var realBlur = true;
			var doUpdate = false;
															   
			//debugger;
			
			if(eventArgs.getEvent().type == "blur")
			{
				var lastKey = -1;
				try{lastKey = grid.cellEditor.lastKeyCode;}catch(e){}
				if(lastKey == 13)
					doUpdate = true;
			}
			else if(keyCode == 38 || keyCode == 40 || keyCode == 13)
				doUpdate = true;
						
			if(doUpdate && grid.datatable.log.selectNodes("//"+nitobi.xml.nsPrefix+"data/*").length !=0)
			{
				grid.BizProc.triggerEvent(grid.ID, 'bizObjUpdated', grid.datatable.log.xml);
				grid.datatable.flushLog();
			}
		}
	}
	catch(e) {}
}

function getSelectedRowAsJSON(gridId){

}

function getGridRowInfo(gridId) {
	var g = nitobi.getComponent(gridId);
	var r = g.getSelectedRow();
	var c = g.columnCount();
	var docEl = nitobi.xml.createXmlDoc('<BizObj />').documentElement;
	for (var p = 0; p < c; p++) {
		var v = "";
		if (g.getColumnObject(p).getModel().getAttribute("editor") == "IMAGE")
			v = g.getCellValue(r, p);
		else {
			v = g.getCellText(r, p).replace(/&nbsp;/g, " ");
			if (v.match(/^\s*$/) != null)
				v = "";
		}
		docEl.setAttribute(g.getColumnObject(p).getModel().getAttribute("ColumnName"), v);
	}
	return docEl;
}
function updateGridRowInfo(gridId, bizobj) {
	var g = nitobi.getComponent(gridId);
	var r = g.getSelectedRow();
	var c = g.columnCount();
	for (var p = 0; p < c; p++) {
		var n = g.getColumnObject(p).getModel().getAttribute("ColumnName");
		var a = bizobj.getAttributeNode(n);
		var nt = g.getColumnObject(p).getModel().getAttribute("editor");
		if (a != null && nt != "LISTBOX" && nt != "CHECKBOX")
			g.getCellObject(r, p).setValue(a.value);						
	}

}
